package com.pronosticnews.client.admin.activity;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.event.shared.EventBus;
import com.pronosticnews.client.admin.ClientFactory;
import com.pronosticnews.client.admin.view.editPays.EditPaysView;
import com.pronosticnews.client.admin.view.editPays.EditPaysView.Presenter;
import com.pronosticnews.client.event.DeletedPaysEvent;
import com.pronosticnews.client.event.DeletedPaysEventHandler;
import com.pronosticnews.client.event.InformationMessageEvent;
import com.pronosticnews.client.event.NouveauPaysEvent;
import com.pronosticnews.client.event.SelectedPaysEvent;
import com.pronosticnews.client.event.SelectedPaysEventHandler;
import com.pronosticnews.client.services.PaysService;
import com.pronosticnews.client.services.PaysServiceAsync;
import com.pronosticnews.client.util.InformationMessage;
import com.pronosticnews.client.util.InformationMessage.TYPE_MESSAGE;
import com.pronosticnews.shared.model.PaysDto;

public class EditPaysActivity extends AbstractActivity implements Presenter {

	private EditPaysView view;

	private PlaceController placeController;

	private PaysServiceAsync paysService;

	private AsyncCallback<PaysDto> saveCallBack;

	private EventBus eventBus;

	private PaysDto existingPays;

	public EditPaysActivity(ClientFactory clientFactory) {
		paysService = GWT.create(PaysService.class);
		placeController = clientFactory.getPlaceController();
		view = clientFactory.getEditPaysView();
		view.setPresenter(this);
		eventBus = clientFactory.getEventBus();

		saveCallBack = new AsyncCallback<PaysDto>() {

			@Override
			public void onSuccess(PaysDto result) {
				InformationMessage messageSuccess = new InformationMessage();
				messageSuccess.setType(TYPE_MESSAGE.SUCCESS);
				List<String> messages = new ArrayList<String>();
				messages.add("Le pays a été créé/modifié avec succès !");
				messageSuccess.setText(messages);
				view.submitComplete();
				eventBus.fireEvent(new NouveauPaysEvent());
				eventBus.fireEvent(new InformationMessageEvent(messageSuccess));
			}

			@Override
			public void onFailure(Throwable caught) {
				InformationMessage erreur = new InformationMessage();
				List<String> messages = new ArrayList<String>();
				messages.add(caught.getMessage());
				erreur.setText(messages);
				erreur.setType(TYPE_MESSAGE.ERROR);
				eventBus.fireEvent(new InformationMessageEvent(erreur));
			}
		};

		eventBus.addHandler(SelectedPaysEvent.TYPE,
				new SelectedPaysEventHandler() {

					@Override
					public void onSelect(SelectedPaysEvent event) {
						existingPays = event.getPaysSelected();
						view.setModeModification(true);
						view.setPays(existingPays);
					}
				});

		eventBus.addHandler(DeletedPaysEvent.TYPE,
				new DeletedPaysEventHandler() {

					@Override
					public void onDelete(DeletedPaysEvent event) {
						existingPays = null;
						view.setModeModification(false);
					}
				});

		view.setModeModification(false);

	}

	@Override
	public void start(AcceptsOneWidget panel,
			com.google.gwt.event.shared.EventBus eventBus) {

		panel.setWidget(view.asWidget());
	}

	@Override
	public void submit(PaysDto editPays) {
		if (existingPays != null) {
			editPays.setId(existingPays.getId());
			paysService.modifier(editPays, saveCallBack);
		} else {
			paysService.sauvegarder(editPays, saveCallBack);
		}
	}

	@Override
	public void annuleModification() {
		existingPays = null;
	}

}
